package com.xykj.deploy.ip2region;

import org.lionsoul.ip2region.xdb.Searcher;

import java.io.IOException;

/**
 * @author Ethan
 * @version 1.0
 * @description: TODO
 * @date 2024-12-28 11:01
 */
public class IP2RegionXDBExample {

    public static void main(String[] args) throws IOException {
        // 1. 指定 ip2region.xdb 文件的路径
        String dbPath = "src/main/resources/ip2region.xdb";

        // 2. 创建 Searcher 对象
        Searcher searcher = null;
        try {
            // 加载 xdb 文件
            byte[] cBuff = Searcher.loadContentFromFile(dbPath);
            searcher = Searcher.newWithBuffer(cBuff);

            // 3. 查询 IP 地址
            String ip = "1.183.20.90";
            String region = searcher.search(ip);

            // 4. 解析查询结果
            System.out.println("IP: " + ip);
            System.out.println("Region: " + region);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 5. 关闭 searcher
            if (searcher != null) {
                try {
                    searcher.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
